package comb;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @Description: 递归实现组合算法
 * @Author: Lynne.
 * @CreateDate: 2019/9/17 15:38
 */

public class CombAlgorithm {
 /* *
     * 场景:输入A,B,C
     * 得到结果：[[A,B],[A,C].[B,C],[A],[B],[C],[A,B,C]]
    */

    public static void main(String[] args) {
        String[] oldArr = {"A", "B", "C"};
        String[][] result = new String[7][];
        String[] strings = new String[2];
        List list = new ArrayList();
        for (int i = 0; i < oldArr.length; i++) {
            /* *
             * 随机生成的新数组
            */
            strings = getComb(oldArr);
            /* *
             * 第一个数组，不用比较，直接存入结果集
            */
            if (i == 0) {
                result[0] = strings;
                /* *
                * 将一个数组里面元素的Ascll码值求和存集合，用来和后面第二个第三个数组做比较
                */
                list.add(getNumber(strings));
            } else {
                /* *
                 * 第二个和第三个数组，要分别和结果集的数组做比较，判断是否已经存在相同的数组
                */
                strings = getArray(strings, result, oldArr, list);
                list.add(getNumber(strings));
                result[i] = strings;
            }

        }
        result = getSingle(oldArr, result);
        getElements(result);
    }


    /* *
   * @Description：从给定数组随机取两个不同的元素生成新数组，并返回
   * @CreateDate:2019/9/18
  */

    private static String[] getComb(String[] oldArr) {
        String[] strings = new String[2];
        Random random = new Random();
        strings[0] = oldArr[random.nextInt(oldArr.length)];
        strings[1] = oldArr[random.nextInt(oldArr.length)];
        int sum = 0;
        if (strings[0] != strings[1]) {
            return strings;
        } else {
            return getComb(oldArr);
        }

    }

/* *
 * @Description:计算出数组中所有元素ASCll码的和，用来判断结果集和新数组是否存在相同元素的依据
*/

    private static int getNumber(String[] str) {
        int sum = 0;
        for (int m = 0; m < str.length; m++) {
            char[] chars = str[m].toCharArray();
            for (int i = 0; i < chars.length; i++) {
                sum += (int) chars[i];
            }
        }
        return sum;


    }


    /* *
     * @Description:判断结果集有没有和当前数组一样的数组
     * @CreateDate:2019/9/18
    */

    private static String[] getArray(String[] strings, String[][] result, String[] oldArr, List<Integer> list) {
        int number = getNumber(strings);
        for (int i = 0; i < list.size(); i++) {
            int temp = getNumber(result[i]);
            if (number != temp) {
                continue;
            } else {
                String[] s = getComb(oldArr);
                return getArray(s, result, oldArr, list);
            }

        }
        return strings;
    }

    /* *
    *遍历输出二维数组
    */

    private static void getElements(String[][] array) {
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            /* *
             * 这个if用来处理最后一个数组逗号的输出
            */
            if(i==array.length-1){
                System.out.print(Arrays.asList(array[i]));
            }else {
                System.out.print(Arrays.asList(array[i])+",");
            }

        }
        System.out.println("]");

    }
    /* *
      * 随机生成三个数组
    */

    private static String[][] getSingle(String[] oldArr, String[][] result) {
        Random random = new Random();
        String a = oldArr[random.nextInt(oldArr.length)];
        String b = oldArr[random.nextInt(oldArr.length)];
        String c = oldArr[random.nextInt(oldArr.length)];
        if (!a.equals(b) && !b.equals(c) && !a.equals(c)) {
            String[] aa = {a};
            String[] bb = {b};
            String[] cc = {c};
            String[] temp = {a, b, c};
            result[3] = aa;
            result[4] = bb;
            result[5] = cc;
            result[6] = temp;
            return result;
        } else {
            return getSingle(oldArr, result);
        }

    }

}